<?php
function getVideos($username=NULL, $udata=NULL){
	/*
	 *RETORNOS
	 *-1 = FALTA DO NOME DE USUARIO
	 *VIDEOS = VIDEOS HÁ SEREM INSERIDOS NO BANCO
	 */


	$old = 0;
	if ($username==NULL){
		return -1;
	}
	if($udata == NULL){
		$old = 1;
	}
	$url = "http://gdata.youtube.com/feeds/api/users/$username/uploads?max-results=50";
	$xml = simplexml_load_file($url);
	$entry = $xml->entry;
	$videos = array();
	for ($i=0; $i < count($entry); $i++) { 
		if($old == 0 and strcmp($udata, $entry[$i]->published) >= 0){
			break;
		}
		$gid = $entry[$i]->id."";
		$ini = strpos($gid, "videos")+7;
		$videos[$i]['vdlabel'] = substr($gid, $ini);
		$videos[$i]['vddata'] = $entry[$i]->published;
		$videos[$i]['vdtitulo'] = $entry[$i]->title;
		$videos[$i]['vddescricao'] = $entry[$i]->content;
	}
	return $videos;
}

function getCanais(){
	$select = "SELECT c.ytid, c.ytusername, c.yttitulo FROM canal c";
	$canais = mysql_query($select);
	return $canais;
}

function setVideos($id, $v){
	for ($i=count($v)-1; $i >= 0; $i--) { 
		$titulo = addslashes($v[$i]['vdtitulo']);
		$descricao = addslashes($v[$i]['vddescricao']);
		$data = $v[$i]['vddata'];
		$label = $v[$i]['vdlabel'];
		$insert = "INSERT INTO video(ytid, vdtitulo, vddescricao, vddata, vdlabel) VALUES('$id','$titulo','$descricao','$data','$label')";
		$res = mysql_query($insert);
		if(!$res){
			return -1;
		}

	}
	return 0;
}

function getData($id){
	return mysql_fetch_array(mysql_query("SELECT MAX(v.vddata) FROM video v WHERE v.ytid = ".$id.""));
}
?>